package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.model.ProductPackBatchImportExcel;
import com.bytz.modules.cms.product.model.ProductPackModel;
import com.bytz.modules.cms.product.service.IAdminProductPackService;
import com.bytz.modules.cms.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "产品打包")
@RequestMapping("/productPack")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductPack.EDIT + "')")
public class ProductPackController {
    @Autowired
    private IAdminProductPackService productPackService;

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<ProductPackModel> queryById(@RequestParam(value = "id") String id) {

        return Result.ok(productPackService.queryById(id));
    }

    @ApiOperation(value = "保存或编辑", notes = "保存或编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<ProductPackModel> saveOrUpdate(@RequestBody ProductPackModel model) {
        productPackService.saveOrUpdateSaveObject(model, false);
        return Result.ok(model);
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ProductPackModel>> page(ProductPackModel packModel,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) throws Exception {

        return Result.ok(productPackService.queryPage(new Page<>(pageNo, pageSize), packModel, req.getParameterMap()));
    }

    @ApiOperation(value = "通过excel获取商品信息", notes = "通过excel获取商品信息")
    @RequestMapping(value = "/getImportProduct", method = RequestMethod.POST)
    public Result<List<Product>> importExcel(HttpServletRequest request) {

        List<String> ids = ExcelUtil.importGroup(request, ProductPackBatchImportExcel.class, null)
                .stream().map(ProductPackBatchImportExcel::getProductNumber)
                .collect(Collectors.toList());

        return Result.ok(productService.getByProductNumbers(ids));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("批量导入到商品包", "Sheet1", ProductPackBatchImportExcel.class, response);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(productPackService.checkAndRemoveById(id));
    }
}
